package base.jfinal;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Record;

public class JsonUtil {
	
	private StringBuffer jsonBuf;
	private boolean isList = false;
	
	public JsonUtil(){jsonBuf = new StringBuffer();}
	
	public JsonUtil(String text){
		jsonBuf = new StringBuffer(text);
		jsonBuf.deleteCharAt(jsonBuf.length()-1);
	}
	
	public JsonUtil(List<Record> recordList){
		isList = true;
		jsonBuf = new StringBuffer();
		jsonBuf.append("[");
		for (int i=0;i<recordList.size();i++){
			Record record = recordList.get(i);
			if (i>0){
				jsonBuf.append(",");
			}
			jsonBuf.append(record.toJson());
		}
		jsonBuf.append("]");
	}

	public JsonUtil(Record record,String arrayColumn){
		isList = false;
		jsonBuf = new StringBuffer();
		Map<String, Object> columnMap =  record.getColumns();
		if (columnMap.size()>0) {
			jsonBuf.append("{");
			for (Map.Entry<String, Object> entry : columnMap.entrySet()) {
				if (entry.getKey().equalsIgnoreCase(arrayColumn)){//数组处理
					if (entry.getValue()==null){
						jsonBuf.append("\"").append(entry.getKey()).append("\":[],");
					}else{
						jsonBuf.append("\"").append(entry.getKey()).append("\":[").append(entry.getValue().toString()).append("],");
					}
				}else{//字符串处理
					if (entry.getValue()==null) {
						jsonBuf.append("\"").append(entry.getKey()).append("\":\"\",");
					}else{
						jsonBuf.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue().toString()).append("\",");
					}
				}
			}
			jsonBuf.deleteCharAt(jsonBuf.length()-1);
			jsonBuf.append("}");
		}
	}

	public JsonUtil(List<Record> recordList,String arrayColumn){
		isList = true;
		jsonBuf = new StringBuffer();
		jsonBuf.append("[");
		for (int i=0;i<recordList.size();i++){
			Record record = recordList.get(i);
			if (i>0){
				jsonBuf.append(",");
			}
			Map<String, Object> columnMap =  record.getColumns();
			if (columnMap.size()>0) {
				jsonBuf.append("{");
				for (Map.Entry<String, Object> entry : columnMap.entrySet()) {
					if (entry.getKey().equalsIgnoreCase(arrayColumn)){//数组处理
						if (entry.getValue()==null){
							jsonBuf.append("\"").append(entry.getKey()).append("\":[],");
						}else if (StringUtils.startsWith(entry.getValue()
								.toString(), "[")) {
							jsonBuf.append("\"").append(entry.getKey()).append(
									"\":").append(entry.getValue().toString())
									.append(",");
						}else{
							jsonBuf.append("\"").append(entry.getKey()).append("\":[").append(entry.getValue().toString()).append("],");
						}
					}else{//字符串处理
						if (entry.getValue()==null) {
							jsonBuf.append("\"").append(entry.getKey()).append("\":\"\",");
						}else{
							jsonBuf.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue().toString()).append("\",");
						}
					}
				}
				jsonBuf.deleteCharAt(jsonBuf.length()-1);
				jsonBuf.append("}");
			}
		}
		jsonBuf.append("]");
	}
	
	public void put(String key,List<Record> recordList){
		if (isList){
			throw new RuntimeException("Illegal operation !");
		}
		if (jsonBuf.length()==0){
			jsonBuf.append("{\"");
		}else{
			jsonBuf.append(",\"");
		}
		jsonBuf.append(key).append("\":[");
		for (int i=0;i<recordList.size();i++){
			Record record = recordList.get(i);
			if (i>0){
				jsonBuf.append(",");
			}
			jsonBuf.append(record.toJson());
		}
		jsonBuf.append("]");
	}
	
	public void put(String key,String value){
		if (isList){
			throw new RuntimeException("Illegal operation !");
		}
		if (jsonBuf.length()==0){
			jsonBuf.append("{\"");
		}else{
			jsonBuf.append(",\"");
		}
		jsonBuf.append(key).append("\":\"").append(value).append("\"");
	}

	public void putArray(String key,String value){
		if (isList){
			throw new RuntimeException("Illegal operation !");
		}
		if (jsonBuf.length()==0){
			jsonBuf.append("{\"");
		}else{
			jsonBuf.append(",\"");
		}
		jsonBuf.append(key).append("\":[").append(value).append("]");
	}
	
	public String toString(){
		if (!isList){
			if (jsonBuf.charAt(jsonBuf.length()-1)!='}'){
				jsonBuf.append("}");
			}
		}
		return jsonBuf.toString().replace("\":null","\":\"\"");
	}
	
}
